package com.example.secondhandsysapi.Controller;


import com.example.secondhandsysapi.Entity.Category;
import com.example.secondhandsysapi.Service.BrowseLogInfoService;
import com.example.secondhandsysapi.Vo.BrowseGoodsInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("browse")
public class BrowseLogInfoController {

    @Autowired
    private BrowseLogInfoService browseLogInfoService;

    // http://localhost:8080/browse/activeUser
    @GetMapping("activeUser")
    public Integer getActiveUser(@PathVariable Date browse_time) {
        return browseLogInfoService.getActiveUserCount(browse_time);
    }


    @GetMapping("/info")
    public List<BrowseGoodsInfo> getInfo(@RequestParam Integer user_id) {
        return browseLogInfoService.getBrowseLogInfo(user_id);
    }

    //通过商品名称查询
    @GetMapping("querybyname")
    public List<BrowseGoodsInfo> getBrowseGoodsByName(@RequestParam Integer user_id,@RequestParam(required = false) String name) {
        List<BrowseGoodsInfo> browseGoodsInfo = null;
        if(name != null){
            // 当商品名称不为空时，根据用户ID和商品名称查询历史浏览记录
            browseGoodsInfo = browseLogInfoService.getBrowseInfoByIdAndName(user_id,name);
        }else {
            // 当商品名称为空时，返回该用户所有当天的历史浏览记录
            browseGoodsInfo = browseLogInfoService.getBrowseLogInfo(user_id);
        }
        return browseGoodsInfo;
    }

    //通过日期查询
    @GetMapping("querybydate")
    public List<BrowseGoodsInfo> getBrowseGoodsByDate(@RequestParam Integer user_id,@RequestParam(required = false) Date date) {
        List<BrowseGoodsInfo> browseGoodsInfo = null;
        if(date != null){
            browseGoodsInfo = browseLogInfoService.getBrowseInfoByIdAndDate(user_id,date);
        }else{
            browseGoodsInfo = browseLogInfoService.getBrowseLogInfo(user_id);
        }
        return browseGoodsInfo;
    }

    @GetMapping("querybytwo")
    public List<BrowseGoodsInfo> getBrowseGoodsByTwo(@RequestParam Integer user_id,@RequestParam(required = false) Date date,@RequestParam(required = false) String name) {
        List<BrowseGoodsInfo> browseGoodsInfo = null;
        if(name != null && date != null){
            browseGoodsInfo = browseLogInfoService.getBrowseInfoByIdAndDateAndName(user_id,date,name);
        }else if(date != null){
            browseGoodsInfo = browseLogInfoService.getBrowseInfoByIdAndDate(user_id,date);
        }else if(name != null){
            browseGoodsInfo = browseLogInfoService.getBrowseInfoByIdAndName(user_id,name);
        }else{
            browseGoodsInfo = browseLogInfoService.getBrowseLogInfo(user_id);
        }
        return browseGoodsInfo;
    }

    // http://localhost:8080/browse/inser
    @GetMapping("inser")
    public Integer insert(@RequestParam(required = false) Integer user_id,@RequestParam(required = false) Integer goods_id) {
        Integer result = browseLogInfoService.insertBrowseLogInfo(user_id, goods_id);
        return 1;
    }

}
